import 'package:flutter/material.dart';
import 'package:flutter_menu/biz/order/order_pg.dart';
import 'package:flutter_menu/data/base_provider.dart';
import 'package:flutter_menu/model/menu_model.dart';
import 'package:flutter_menu/model/product_model.dart';
import 'package:flutter_menu/objectbox.g.dart';

class ElectronicProvider extends BaseProvider<ElectronicMenuModel> {
  ElectronicProvider(super.store);

  Future<List<ElectronicMenuModel>> getElecMenuList() {
    return box.getAllAsync();
  }

  /// 0. 过滤 `ElectronicMenuModel`，仅保留 `productList.imageUrl` 不为空的 customMenuUuid 集合
  Future<List<String>> filterMenusWithImages() async {
    final allMenus = await box.getAllAsync();
    return allMenus
        .where((menu) {
          return menu.productList
                  .any((product) => product.imageUrl?.isNotEmpty ?? false) ??
              false;
        })
        .map((menu) => menu.customMenuUuid)
        .whereType<String>()
        .toSet()
        .toList();
  }

  void putDemoData() {
    if (box.isEmpty()) {
      clear();
      insertManyFromJson().then((value) {
        debugPrint("insertManyElectronicFromJson $value");
      }, onError: (err) {
        debugPrint("insertManyElectronicFromJson error $err");
      });
    }
  }

  // 批量插入 JSON 列表
  Future<List<int>> insertManyFromJson() {
    List<dynamic> demoJson = [
      {
        "name": "菜单1",
        "customMenuUuid": "B1FAAA4F10",
        "isRead": 1,
        "productList": [
          {
            "productUuid": "833006101edc4b18b61e55224912ba4a",
            "imageUrl": testImageUrl,
            "imageName": "土豆茄子图",
          },
          {
            "productUuid": "0c297b8d400b409690f04da551314b68",
            "imageUrl": "https://example.com/image1.png",
            "imageName": "错图",
          },
          {"productUuid": "30fd4f4fc6d6465c84247faef9b442b6", "imageUrl": "https://img2.baidu.com/it/u=2699260016,3014692979&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=749", "imageName": "Mojito",},
          {"productUuid": "1c4996ff4b7d4806af48119fb48323b8", "imageUrl": "https://img2.baidu.com/it/u=2699260016,3014692979&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=749", "imageName": "Mojito 套餐",},
          {"productUuid": "9e7181978608411384c3ea5f8b8d0561", "imageUrl": "https://img2.baidu.com/it/u=2699260016,3014692979&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=749", "imageName": "家常 可选 家套餐",},
        ]
      },
      {
        "name": "菜单2",
        "customMenuUuid": "CCE48FF665",
        "isRead": 1,
        "productList": [
          {
            "productUuid": "b98fa3c5f1c045a8b2e9e9c4c507a999",
            "imageUrl": testImageUrl,
            "imageName": "多规格的分类",
            "hotZone": '{"left": -0.1, "right": 1.2, "top": "invalid hot zone", "bottom": 0.5}'
          },
          {
            "productUuid": "833006101edc4b18b61e55224912ba4b",
            "imageUrl":
                "https://img2.baidu.com/it/u=2626932232,1159932584&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=1069",
            "imageName": "不一样的蔡明",
            "hotZone": '{"left": 0.2, "right": 0.8, "top": 0.1, "bottom": 0.9}'
          },
          {
            "productUuid": "0c297b8d400b409690f04da551314b68",
            "imageUrl": "https://example.com/image1.png",
            "imageName": "不一样的蔡明1",
          },
          {"productUuid": "30fd4f4fc6d6465c84247faef9b442b6", "imageUrl": ""},
        ]
      },
      {
        "name": "菜单3",
        "customMenuUuid": "uuid3",
        "productList": [
          {"imageUrl": ""}
        ]
      }
    ];
    // final categories = demoJson.map((json) => CategoryModel.fromJson(json)).toList();
    final categories = ElectronicMenuModel.toManyFromJson(demoJson);
    return putManyAsync(categories);
  }
}
